/**
 * [90] 子集 II
 * 提示：
 * # 1 <= nums.length <= 10
 * # -10 <= nums[i] <= 10
 * @format
 * @lc app=leetcode.cn id=90 lang=javascript
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
// 递归遍历分支
var subsetsWithDup = function (nums) {
    nums.sort((a, b) => a - b)
    function forNums(nums) {
        const N = nums.length
        if (N == 1) return [[nums[0]]]
        const results = [[nums[0]], ...forNums(nums.slice(1)).map(e => [nums[0], ...e])]
        // console.log(results)
        for (let i = 1; i < N; i++) {
            const TOP = results.length - 1
            // 去重
            if (nums[i] == results[TOP][0]) continue
            else if (i == N - 1) results.push([nums[i]])
            else results.push([nums[i]], ...forNums(nums.slice(i + 1)).map(e => [nums[i], ...e]))
            // console.log(results)
        }
        return results
    }
    return [[], ...forNums(nums)]
}
// 排序（转字符串去重）+二进制表
// @lc code=end
// debugger
// const nums = [1, 2]
// const results = subsetsWithDup(nums)
// console.log(results)
